#include <cstdlib>
#include <ctime>
#include "rclcpp/rclcpp.hpp"
#include "base_interfaces_demo/srv/patrol.hpp"
#include <chrono>
using namespace std::chrono_literals;
using base_interfaces_demo::srv::Patrol;
class TurtlePatrolClient:public rclcpp::Node
{
public:
    TurtlePatrolClient():Node("turtle_patrol_client_node")
    {
        patrol_client_=this->create_client<Patrol>("patrol");
        timer_=this->create_wall_timer(10s,std::bind(
            &TurtlePatrolClient::timer_call_back,this));
        srand(time(NULL));  
    }
    void timer_call_back()
    {
        while(!patrol_client_->wait_for_service(1s))
        {
            if(!rclcpp::ok())
            {
                RCLCPP_ERROR(this->get_logger(),"等待服务过程中被打断...");
            }
            RCLCPP_INFO(this->get_logger(),"等待服务上线");
        }
        auto request=std::make_shared<Patrol::Request>();
        request->target_x=rand()%15;
        request->target_y=rand()%15;
        RCLCPP_INFO(this->get_logger(),"请求巡逻：{%f,%f}",
        request->target_x,request->target_y);
        patrol_client_->async_send_request(
            request,
            [&](rclcpp::Client<Patrol>::SharedFuture result_future)->void
            {
                auto response=result_future.get();
                if(response->result==Patrol::Response::SUCCESS)
                {
                    RCLCPP_INFO(this->get_logger(),"目标点处理成功");
                }
                else if(response->result==Patrol::Response::FALL)
                {
                    RCLCPP_INFO(this->get_logger(),"目标点处理失败");
                }
            }
            
            
            );

    }

private:
    rclcpp::TimerBase::SharedPtr timer_;
    rclcpp::Client<Patrol>::SharedPtr patrol_client_;    
};
int main(int argc,char*argv[])
{
    rclcpp::init(argc,argv);
    rclcpp::spin(std::make_shared<TurtlePatrolClient>());
    rclcpp::shutdown();
    return 0;
}